package com.zhiche.lisa.oms.controller;

import com.alibaba.fastjson.JSONObject;
import com.zhiche.lisa.core.enums.SystemEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.core.utils.redislog.RedisLogUtil;
import com.zhiche.lisa.oms.dto.HuiyuncheCancleDTO;
import com.zhiche.lisa.oms.dto.HuiyuncheShipInfoDTO;
import com.zhiche.lisa.oms.service.IHuiyuncheService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * 慧运车人送--oms指令推送
 */
@RestController
@RequestMapping("/omsHuiyunche")
@Api(value = "/omsHuiyunche", description = "人送指令接口",tags={"人送指令接口"})
public class HuiyuncheController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private IHuiyuncheService huiyuncheService;

    @Value("${logRedis.isTest}")
    private boolean isTest;

    /**
     * 指令创建运单订单
     */
    @PostMapping("/shipInfo")
    @ApiOperation(value = "指令创建运单订单", notes = "指令创建运单订单", produces = MediaType.APPLICATION_JSON_VALUE,response = RestfulResponse.class)
    public RestfulResponse<Object> shipInfoSync(@RequestBody HuiyuncheShipInfoDTO infoDTO) {
        RestfulResponse<Object> result = new RestfulResponse<>(0, "操作成功", null);
        logger.info("人送指令同步---OMS param:{}", JSONObject.toJSONString(infoDTO));
        try {

            //记录指令已接收到日志系统
            RedisLogUtil.addSysShipmentLogs(SystemEnum.INTEGRATION.getCode(), SystemEnum.OMS.getCode(), "/omsHuiyunche/shipInfo", infoDTO.getVcorderno(),
                    null, JSONObject.toJSONString(infoDTO), 2, 1, "", isTest);


            huiyuncheService.shipInfoSync(infoDTO, null);

            //记录指令已接收到日志系统
            RedisLogUtil.addSysShipmentLogs(SystemEnum.INTEGRATION.getCode(), SystemEnum.OMS.getCode(), "/omsHuiyunche/shipInfo", infoDTO.getVcorderno(),
                    null, JSONObject.toJSONString(infoDTO), 3, 1, "", isTest);

        } catch (BaseException e) {
            RedisLogUtil.addSysShipmentLogs(SystemEnum.INTEGRATION.getCode(), SystemEnum.OMS.getCode(), "/omsHuiyunche/shipInfo", infoDTO.getVcorderno(),
                    null, JSONObject.toJSONString(infoDTO), 3, 0, e.getMessage(), isTest);

            logger.error("人送指令同步---OMS BaseException:{}", e);
            result.setCode(-1);
            result.setMessage(e.getMessage());
        } catch (Exception e) {
            RedisLogUtil.addSysShipmentLogs(SystemEnum.INTEGRATION.getCode(), SystemEnum.OMS.getCode(), "/omsHuiyunche/shipInfo", infoDTO.getVcorderno(),
                    null, JSONObject.toJSONString(infoDTO), 3, 0, e.getMessage(), isTest);
            logger.error("人送指令同步---OMS error:{}", e);
            result.setCode(-1);
            result.setMessage("系统异常");
        }
        return result;
    }


    /**
     * 指令退单
     */
    @PostMapping("/cancelShip")
    @ApiOperation(value = "指令退单", notes = "指令退单", produces = MediaType.APPLICATION_JSON_VALUE,response = RestfulResponse.class)
    public RestfulResponse<Object> shipInfoSync(@RequestBody HuiyuncheCancleDTO infoDTO) {
        RestfulResponse<Object> result = new RestfulResponse<>(0, "操作成功", null);
        logger.info("人送退单同步---OMS param:{}", JSONObject.toJSONString(infoDTO));
        try {

            //系统交互接收日志记录
            RedisLogUtil.addSysLogs(SystemEnum.INTEGRATION.getCode(), SystemEnum.OMS.getCode(), infoDTO.getOrderCode(), JSONObject.toJSONString(infoDTO),
                    "/omsHuiyunche/cancelShip", 2, 1, "", isTest);

            huiyuncheService.cancelShip(infoDTO, null);

            //系统交互处理日志记录（成功）
            RedisLogUtil.addSysLogs(SystemEnum.INTEGRATION.getCode(), SystemEnum.OMS.getCode(), infoDTO.getOrderCode(), JSONObject.toJSONString(infoDTO),
                    "/omsHuiyunche/cancelShip", 3, 1, "", isTest);
        } catch (BaseException e) {
            //系统交互处理日志记录（失败）
            RedisLogUtil.addSysLogs(SystemEnum.INTEGRATION.getCode(), SystemEnum.OMS.getCode(), infoDTO.getOrderCode(), JSONObject.toJSONString(infoDTO),
                    "/omsHuiyunche/cancelShip", 3, 0, e.getMessage(), isTest);

            logger.error("人送退单同步---OMS BaseException:{}", e);
            result.setCode(-1);
            result.setMessage(e.getMessage());
        } catch (Exception e) {
            //系统交互处理日志记录（失败）
            RedisLogUtil.addSysLogs(SystemEnum.INTEGRATION.getCode(), SystemEnum.OMS.getCode(), infoDTO.getOrderCode(), JSONObject.toJSONString(infoDTO),
                    "/omsHuiyunche/cancelShip", 3, 0, "系统异常", isTest);

            logger.error("人送退单同步---OMS error:{}", e);
            result.setCode(-1);
            result.setMessage("系统异常");
        }
        return result;
    }

}
